package filter;

import Pojo.Site;
import ViewOption.siteVo;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "setTitleFilter", urlPatterns = {
        "/login.jsp",
        "/center/*",
        "/admin/*"
})
public class setTitleFilter implements Filter {
    private final siteVo siteVo = new siteVo();

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession session = request.getSession();
        String reqUri = request.getRequestURI().replace(request.getContextPath(), "");
        String index = request.getParameter("index") != null ? request.getParameter("index") : "";
        System.out.println(reqUri);
        Site site = (Site) siteVo.getSiteAll();
        session.setAttribute("sitetitle", site.getSiteTitle());
        if (reqUri.equals("/login.jsp")) {
            session.setAttribute("sitetitle", "登录界面");
        } else if (reqUri.equals("/center/")) {
            if (index.equals("userSafe")) {
                session.setAttribute("sitetitle", "安全中心");
            } else if (index.equals("addCreditCard")) {
                session.setAttribute("sitetitle", "添加信用卡");
            } else if (index.equals("manageCreditCard")) {
                session.setAttribute("sitetitle", "管理信用卡");
            } else {
                session.setAttribute("sitetitle", "个人中心");
            }
        } else if (reqUri.equals("/admin/")) {
            if (index.equals("managerUsers")) {
                session.setAttribute("sitetitle", "用户管理");
            } else if (index.equals("managerCreditCard")) {
                session.setAttribute("sitetitle", "信用卡管理");
            } else if (index.equals("manageBank")) {
                session.setAttribute("sitetitle", "银行管理");
            } else {
                session.setAttribute("sitetitle", "站点设置");
            }
        }

        session.setAttribute("subTitle", site.getSiteSubTitle());
        session.setAttribute("siteContent", site.getSiteContent());
        session.setAttribute("siteKeyword", site.getSiteKeyword());
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
